<?php

namespace Portal\Domain;

class Role
{
    public function getRoleIds($roles)
    {
        $cacheKey = 'Role.getRoleIds.'.json_encode($roles);
        $cacheData = \PhalApi\DI()->cache->get($cacheKey);
        if ($cacheData !== null) {
            return $cacheData;
        }
        $datas = \Portal\Model\Role::notorm()->select('id')->where(['code' => $roles,'status' => 1])->fetchAll();
        $returnData = [];
        foreach ($datas as $data) {
            $returnData[] = $data['id'];
        }
        \PhalApi\DI()->cache->set($cacheKey, $returnData);
        return $returnData;
    }

    public function getRoleNames($userId)
    {
        $cacheKey = 'Role.getRoleNames.'.$userId;
        $cacheData = \PhalApi\DI()->cache->get($cacheKey);
        if ($cacheData !== null) {
            return $cacheData;
        }
        $datas = \Portal\Model\AdminRole::notorm()->select('role_id')->where(['user_id' => $userId])->fetchAll();
        $returnData = '';
        $roleIds = [];
        if (is_array($datas) && !empty($datas)) {
            foreach ($datas as $data) {
                $roleIds[] = $data['role_id'];
            }
            $roleDatas = \Portal\Model\Role::notorm()->select('name')->where(['id' => $roleIds])->fetchAll();
            if (is_array($roleDatas) && !empty($roleDatas)) {
                $roleNames = [];
                foreach ($roleDatas as $roleData) {
                    $roleNames[] = $roleData['name'];
                }
                $returnData = implode(',', $roleNames);
                \PhalApi\DI()->cache->set($cacheKey, $returnData);
            }
        }
        return $returnData;
    }

    public function getOptions()
    {
        $returnData = [];
        $datas = \Portal\Model\Role::notorm()->select('id AS value,name AS label,status AS tag')->where(['status' => 1,'is_deleted' => 0])->order('sort', 'id')->fetchAll();
        if (is_array($datas) && !empty($datas)) {
            foreach ($datas as $data) {
                if ($data['tag'] == '1') {
                    $data['tag'] = 'success';
                } else {
                    $data['tag'] = 'danger';
                }
                $returnData[] = $data;
            }
        }
        return $returnData;
    }

    public function getRoleCodes($userId)
    {
        // $cacheKey='Role.getRoleCodes.'.$userId;
        // $cacheData= \PhalApi\DI()->cache->get($cacheKey);
        // if ($cacheData!==null) {
        //     return $cacheData;
        // }
        $datas = \Portal\Model\AdminRole::notorm()->select('role_id')->where(['user_id' => $userId])->fetchAll();
        $returnData = '';
        $roleIds = [];
        if (is_array($datas) && !empty($datas)) {
            foreach ($datas as $data) {
                $roleIds[] = $data['role_id'];
            }
            //ROOT 拥有所有权限

            $roleDatas = \Portal\Model\Role::notorm()->select('code')->where(['id' => $roleIds])->fetchAll();
            if (is_array($roleDatas) && !empty($roleDatas)) {
                $roleCodes = [];
                foreach ($roleDatas as $roleData) {
                    $roleCodes[] = $roleData['code'];
                }
                $returnData = $roleCodes;
                // \PhalApi\DI()->cache->set($cacheKey, $returnData);
            }
        }
        return $returnData;
    }

    public function minDataScope($roles)
    {
        $minDataScope = 4;
        if (is_array($roles) && !empty($roles)) {
            $minDataScopeData = \Portal\Model\Role::notorm()->select('min(data_scope) AS minDataScope')->where(['code' => $roles,'is_deleted' => 0])->fetchOne();
            if ($minDataScopeData) {
                $minDataScope = intval($minDataScopeData['minDataScope']);
            }
        }
        $minDataScope = max(1, $minDataScope);
        return $minDataScope;
    }
}
